import java.util.*;

public class MemoryTest {
    public static int count = 1000;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Cold start");
        testArray();

        System.out.println("Array");
        testArray();

        System.out.println("AL");
        testAL();

        System.out.println("LL");
        testLL();

        System.out.println("HM");
        testHM();

        System.out.println("TM");
        testTM();
    }

    public static void testArray() {
        int i = 0;
        long freeMemory = 0;
        long totalMemory = 0;

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory();
        totalMemory = Runtime.getRuntime().totalMemory();

        int[] array = new int[count];
        for (i = 0; i < count; i++) {
            array[i] = i;
        }

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory() - freeMemory;
        totalMemory = Runtime.getRuntime().totalMemory() - totalMemory;
        System.out.println(-freeMemory);
        System.out.println(totalMemory);
    }

    public static void testAL() {
        int i = 0;
        long freeMemory = 0;
        long totalMemory = 0;

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory();
        totalMemory = Runtime.getRuntime().totalMemory();

        List<Integer> array = new ArrayList<>(count);
        for (i = 0; i < count; i++) {
            array.add(i);
        }

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory() - freeMemory;
        totalMemory = Runtime.getRuntime().totalMemory() - totalMemory;
        System.out.println(-freeMemory);
        System.out.println(totalMemory);
    }

    public static void testLL() {
        int i = 0;
        long freeMemory = 0;
        long totalMemory = 0;

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory();
        totalMemory = Runtime.getRuntime().totalMemory();

        List<Integer> array = new LinkedList<>();
        for (i = 0; i < count; i++) {
            array.add(i);
        }

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory() - freeMemory;
        totalMemory = Runtime.getRuntime().totalMemory() - totalMemory;
        System.out.println(-freeMemory);
        System.out.println(totalMemory);
    }

    public static void testHM() {
        int i = 0;
        long freeMemory = 0;
        long totalMemory = 0;

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory();
        totalMemory = Runtime.getRuntime().totalMemory();

        Map<Integer, Integer> array = new HashMap<>(count);
        for (i = 0; i < count; i++) {
            array.put(i, i);
        }

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory() - freeMemory;
        totalMemory = Runtime.getRuntime().totalMemory() - totalMemory;
        System.out.println(-freeMemory);
        System.out.println(totalMemory);
    }

    public static void testTM() {
        int i = 0;
        long freeMemory = 0;
        long totalMemory = 0;

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory();
        totalMemory = Runtime.getRuntime().totalMemory();

        Map<Integer, Integer> array = new TreeMap<>();
        for (i = 0; i < count; i++) {
            array.put(i, i);
        }

        System.gc();
        freeMemory = Runtime.getRuntime().freeMemory() - freeMemory;
        totalMemory = Runtime.getRuntime().totalMemory() - totalMemory;
        System.out.println(-freeMemory);
        System.out.println(totalMemory);
    }
}
